package com.accompany.fragment.order.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.accompany.R;
import com.accompany.api.bean.OrderDetialBean;
import com.accompany.api.bean.OrderMesgBean;
import com.accompany.api.category.OrderDetialCatgory;
import com.accompany.api.presenter.OrderDetialPresenter;
import com.accompany.base.BaseActivity;
import com.accompany.base.BaseBean;
import com.accompany.dialog.RoundMsgHeduiDialog;
import com.accompany.dialog.RoundOrderCompDialog;
import com.accompany.main.ConstantStaticUrl;
import com.accompany.utils.FullScreenUtils;
import com.accompany.utils.HttpNetworkConnectUtils;
import com.accompany.utils.SPUtils;
import com.accompany.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 信息说明:  服务进程
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/5/10 15 ：03
 * 项目名称：yuanban
 */
public class OrderServicectivity extends BaseActivity implements OrderDetialCatgory.View {
    @BindView(R.id.base_title)
    TextView mTitleMsg;
    @BindView(R.id.fwu_xiagm)
    TextView mServiceNmse;
    @BindView(R.id.service_yiyuan)
    TextView mServieYiyuan;
    @BindView(R.id.service_time)
    TextView mServiceTime;
    @BindView(R.id.base_back)
    ImageView baseBack;
    @BindView(R.id.base_setting)
    ImageView baseSetting;
    @BindView(R.id.toolbar)
    RelativeLayout toolbar;
    @BindView(R.id.real_jiuzhen)
    RelativeLayout realJiuzhen;
    @BindView(R.id.one_line)
    View oneLine;
    @BindView(R.id.two_line)
    View twoLine;
    @BindView(R.id.real_comp)
    RelativeLayout realComp;
    @BindView(R.id.tv_step)
    TextView tvStep;
    @BindView(R.id.tv_lastStep)
    TextView tvLastStep;
    private Long orderId;
    private String mToken;
    private OrderDetialPresenter mPresenter;
    private String mServiDateTime;
    private String mYiyuanName;
    private String mServiceMsg;
    private String mPaintName;
    private String mPhoneMobile;
    private long mOrderId;
    private RoundOrderCompDialog dialog1;
    private boolean firstStep = false, secondStep = false;
    private OrderMesgBean.OrderMesgBeanData.OrderMesgBeanDataList orderMesgBeanDataList;
    // 4 服务中   5已完成
    private boolean isCheckFirst;//先只能点击第一步时间轴   才能点击第二步
    private int orderStaus= -1;

    @Override
    public int getLayoutId() {
        return R.layout.activity_order_service_view;
    }

    @Override
    public void initLayoutView() {
        mTitleMsg.setText("服务进程");
        FullScreenUtils.getInstance().fullScreen(OrderServicectivity.this, true);
        orderId = getIntent().getLongExtra("orderId",0);
//        orderMesgBeanDataList= (OrderMesgBean.OrderMesgBeanData.OrderMesgBeanDataList) getIntent().getSerializableExtra("item");
//        mYiyuanName = orderMesgBeanDataList.getOrderDTO().getHospitalName();
//        mServiDateTime = orderMesgBeanDataList.getOrderDTO().getPeriodStr();
//        mServiceMsg = orderMesgBeanDataList.getTitle();
//        mServiceNmse.setText(mServiceMsg);
//        mServieYiyuan.setText(mYiyuanName);
//        mServiceTime.setText(mServiDateTime);
        mToken = (String) SPUtils.get(OrderServicectivity.this, ConstantStaticUrl.mToken, "");
        mPresenter = new OrderDetialPresenter(this);
        SetPersenter(mPresenter);
        if (!HttpNetworkConnectUtils.isNetworkConnected(OrderServicectivity.this)) {
            ToastUtil.showToastERROR(OrderServicectivity.this, "网络不给力,请稍后重试");
        } else {
            mPresenter.getOrderDetial(mToken, orderId);
        }
    }

    @OnClick({R.id.base_back, R.id.real_jiuzhen, R.id.real_comp})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finishWitchAnimation();
                break;
            case R.id.real_jiuzhen:
                initJiuzhenPersonDialog();//时间轴第一步
                break;
            case R.id.real_comp:
//                if (){
//
//                }
                if (isCheckFirst ){
                    initOrderCompleteDialog();// 结束时间轴
                }else {
                    ToastUtil.showToastINFO(OrderServicectivity.this, "请先核对信息");
                }
                break;
        }
    }

    private void initOrderCompleteDialog() {
        dialog1 = new RoundOrderCompDialog(this);
        dialog1.show();
        TextView comp_name = (TextView) dialog1.findViewById(R.id.comp_name);
        TextView comp_xiangmu = (TextView) dialog1.findViewById(R.id.comp_xiangmu);
        TextView comp_yiyuan = (TextView) dialog1.findViewById(R.id.comp_yiyuan);
        TextView comp_time = (TextView) dialog1.findViewById(R.id.comp_time);
        comp_time.setText(mServiDateTime);
        comp_yiyuan.setText(mYiyuanName);
        comp_xiangmu.setText(mServiceMsg);
        comp_name.setText(mPaintName);
        dialog1.findViewById(R.id.comp_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog1.dismiss();
            }
        });
        dialog1.findViewById(R.id.comp_compp).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.getCompleteOrder(mToken, orderId);
                dialog1.dismiss();
            }
        });

    }

    private void initJiuzhenPersonDialog() {
        RoundMsgHeduiDialog dialog = new RoundMsgHeduiDialog(this);
        dialog.show();
        TextView mName = (TextView) dialog.findViewById(R.id.order_name);
        TextView ordre_fuw = (TextView) dialog.findViewById(R.id.ordre_fuw);
        TextView ordre_yiyuan = (TextView) dialog.findViewById(R.id.ordre_yiyuan);
        TextView order_time = (TextView) dialog.findViewById(R.id.order_time);
        order_time.setText(mServiDateTime);
        ordre_yiyuan.setText(mYiyuanName);
        ordre_fuw.setText(mServiceMsg);
        mName.setText(mPaintName);
        dialog.findViewById(R.id.order_call_phone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 检查是否获得了权限
                if (ContextCompat.checkSelfPermission(OrderServicectivity.this,
                        Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // 若没有获得授权，申请授权
                    if (ActivityCompat.shouldShowRequestPermissionRationale(OrderServicectivity.this,
                            Manifest.permission.CALL_PHONE)) {
                        // 如果app之前请求过该权限,被用户拒绝, 这个方法就会返回true.
                        //  如果用户之前拒绝权限的时候勾选了对话框中”Don’t ask again”的选项,那么这个方法会返回false.
                        // 如果设备策略禁止应用拥有这条权限, 这个方法也返回false.
                        // 弹窗需要解释为何需要该权限，再次请求授权
                        // 帮跳转到该应用的设置界面，让用户手动授权
                        Intent call = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", OrderServicectivity.this.getPackageName(), null);
                        call.setData(uri);
                        startActivity(call);
                    } else {
                        // 不需要解释为何需要该权限，直接请求授权
                        ActivityCompat.requestPermissions(OrderServicectivity.this,
                                new String[]{Manifest.permission.CALL_PHONE},
                                0);
                    }
                } else {
                    // 已经获得授权，可以打电话
                    // 通过intent跳转到拨打电话的界面
                    Intent intent1 = new Intent();
                    intent1.setAction(Intent.ACTION_CALL);
                    Uri phoneNum = Uri.parse("tel:" + mPhoneMobile);
                    intent1.setData(phoneNum);
                    startActivity(intent1);
                }
                dialog.dismiss();
            }
        });
        dialog.findViewById(R.id.orer_quere).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 第一步通过 ,更改时间轴状态
                changeOneStep();
                isCheckFirst = true;
                dialog.dismiss();
            }
        });
    }

    @Override
    public void getOrderDetial(OrderDetialBean baseBean) {
        if (baseBean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            mServiceNmse.setText(baseBean.getData().getOrderDTO().getServiceName());
            mServieYiyuan.setText(baseBean.getData().getOrderDTO().getHospitalName());
            mServiceTime.setText(baseBean.getData().getOrderDTO().getPeriodStr());
            mServiDateTime = baseBean.getData().getOrderDTO().getPeriodStr();
            mYiyuanName = baseBean.getData().getOrderDTO().getHospitalName();
            mServiceMsg = baseBean.getData().getOrderDTO().getServiceName();
            mPaintName = baseBean.getData().getOrderDTO().getPatientName();
            mPhoneMobile = baseBean.getData().getOrderDTO().getPatientPhone();
            mOrderId = baseBean.getData().getId();
            orderStaus = baseBean.getData().getOrderStatus();
            if (orderStaus == 4){
                isCheckFirst = true;
            }
            if (baseBean.getData().getOrderStatus() == 4){
                //  OrderStatus   1-待支付2-待接单（已付款）3-待服务（已接单）4-服务中5-完成6-已取消7-退款
                changeOneStep();

            }else if(baseBean.getData().getOrderStatus() == 3){

            }

        } else {
            ToastUtil.showToastINFO(OrderServicectivity.this, baseBean.getMsg());
        }
    }

    @Override
    public void getStartService(BaseBean baseBean) {

    }

    @Override
    public void getCompleteOrder(BaseBean bean) {
        if (bean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            ToastUtil.showToastSUCCESS(OrderServicectivity.this, "订单已完成");
            changeTwoStep();
            finish();
        } else {
            ToastUtil.showToastINFO(OrderServicectivity.this, bean.getMsg());
        }

    }

    //改变时间轴第一步状态
    private void changeOneStep(){
        firstStep = true;
        oneLine.setBackgroundResource(R.drawable.shape_red_line);
        tvStep.setBackgroundResource(R.drawable.circle_red_bg);
        tvStep.setTextColor(Color.parseColor("#FFFFFF"));
        twoLine.setBackgroundResource(R.drawable.shape_red_line);
    }

    //改变时间轴第二步状态
    private void changeTwoStep(){
        secondStep = true;
        tvLastStep.setBackgroundResource(R.drawable.circle_red_bg);
        tvLastStep.setTextColor(Color.parseColor("#FFFFFF"));
    }



    @Override
    public void SetPersenter(Object persenter) {
    }

    @Override
    public void OnShowErrorMsg(String mErrorMsg) {
        ToastUtil.showToastERROR(OrderServicectivity.this, mErrorMsg);
    }


}
